package pl.pawluk.inz.web;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

import javax.faces.context.FacesContext;

import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Component;

/**
 * @author mandp
 *
 */
@Component
@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS)
public class LocaleBean implements Serializable {

    /**
     *
     */
    private static final long serialVersionUID = 4079009107945921120L;

    private Map<String, Locale> locales = null;

    private Locale locale;


    public LocaleBean() {
        FacesContext ctx = FacesContext.getCurrentInstance();
        if (ctx != null) {
            locale = ctx.getViewRoot().getLocale();
        }
        locales = new HashMap<String, Locale>(2);
        locales.put("PL", new Locale("pl", "PL"));
        locales.put("UK", new Locale("en", "UK"));
    }


    public void setLocale(String current) {
        FacesContext context = FacesContext.getCurrentInstance();
        context.getViewRoot().setLocale(locales.get(current));
        context.getApplication().setDefaultLocale(locales.get(current));
        ResourceBundle.clearCache();
        locale = locales.get(current);
    }


    public Locale getLocale() {
        return locale;
    }


    public void setLocale(Locale locale) {
        this.locale = locale;
    }
}
